<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>应急信息发布项目维护添加</title>
    
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link href="../../layui/css/iconfont/iconfont.css" rel="stylesheet" type="text/css">
    <link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <link href="../../css/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" type="text/css">
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.8.3.js"></script>
    <script src="../../js/util.js"></script>
    <script src="../../js/jquery-ui-1.10.4.custom.min.js"></script>
    <link href="../../layui/formSelects-v4.css" rel="stylesheet" />
	<script src="../../layui/formSelects-v4.js"></script>
    
    <script>
        layui.use(['table', 'form', 'jquery', 'element','layer','laydate','upload'], function () {
            var table = layui.table;
            var form = layui.form;
            var $ = layui.$;
            var upload = layui.upload;
            var layer=layui.layer;
            var user;
            var flagMgr;
            var laydate = layui.laydate;
            var mapManage = "/mapManage.html";
            var stationIsRepeat = false;
	        laydate.render({
	             elem: '#startTime'
	             , type: 'datetime'
	             ,min:minDate()
	        });
	        laydate.render({
	             elem: '#endTime'
	             , type: 'datetime'
	             ,min:minDate()
	        });
	        
	        function minDate(){
			    var now = new Date();
			    return now.getFullYear()+"-" + (now.getMonth()+1) + "-" + now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
			}
			
			
            //根据城市ID查询扩展码
            function selectExtInfo(cityId) {
                var data;
                var url = '/util/findExdId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            console.log(json.data);
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接扩展码
            function appendExtId(data) {
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i] + '">' + data[i] + '</option>';
                    }
                    $('#extId').html(options);
                } else {
                    $('#extId').html('<option value="">该地市下没有扩展码</option>');
                }
                form.render();
            }
            //监听区县
            form.on('select(cityId)', function (data) {
                var data2 = selectCountyId(data.value);
                appendCounty(data2);
                //appendCountyCheckBox(data2);
                var data3 = selectExtInfo(data.value);
                appendExtId(data3);
            });
            //根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接区县
            function appendCounty(data){
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
                    }
                    $('#countyId').html(options);
                } else {
                    $('#countyId').html('该地市下没有区县');
                }
                form.render();
            }
            
            //
            function qryPathName() {
                var data;
                var url = '/smsArea/findPathName';
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                            if (data.length != null) {
                    		var options = '<option value="all">全部</option>';
                    			for (var i = 0; i < data.length; i++) {
                    				//console.info(data[i]);
                        			options += '<option value="' + data[i] + '">' + data[i] + '</option>';
                    			}
                    			$('#pathName').html(options);
                			} else {
                    			$('#pathName').html('暂无路线信息');
                			}
                form.render();
                            
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            
            form.on('select(pathName)', function (data) {
                var subStationList=selectRegionName(data.value);
                appendSubStationNames(subStationList);
            });
            
            function selectRegionName(pathName) {
                var data;
                var url = '/smsArea/findRegionName?pathName=' + pathName;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            
             function appendSubStationNames(data){
                if (data.length != null) {
                    var options = '<option value="all">全部</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i]+ '">' + data[i] + '</option>';
                    }
                    $('#subStationName').html(options);
                } else {
                    $('#subStationName').html('该路线下暂无点位');
                }
                form.render();
    		}
    
    			$("#regionName").autocomplete({
    			source : function(request, response) {
    				var pathName = $("#pathName").val();
    				var regionName = $("#regionName").val();
    				if("all"===pathName){
    					pathName="";
    				}
    				console.info(regionName);
    				var url = '/smsArea/findRegionNameLike';
    				var param = {
    					pathName : pathName,
    					regionName : regionName
    				};
    				$.ajax({
    					type : 'post',
    					async : false,
    					url : url,
    					data : param,
    					success : function(json) {
    						var data = json.data;
    						source = [];
    						for (var i = 0; i < data.length; i++) {
    							source[i] = data[i];
    						}
    						console.log(source);
    						response(source);
    					},
    					error : function() {
    						layer.msg('系统异常，请联系管理员处理!');
    					}
    				}
    				);
    			},
    			messages : { //message设置不出现匹配关键字的结果
    				noResults : '',
    				results : function() {}
    			}
    		});
    
    
    		//监听项目
    		form.on('select(countyId)', function (data) {
                //var data1 = selectMgrInfo(data.value);
                //appendMgr(data1);
            });
            //根据城市ID和区县查询项目
            function selectMgrInfo(countyId) {
                var data;
                var url = '/util/findByMgrId?countyId=' + countyId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接项目
            function appendMgr(data) {
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
                    }
                    $('#mgrId').html(options);
                } else {
                    $('#mgrId').html('<option value="">该区县下没有项目</option>');
                }
                form.render();
            }
            
            
            
            $('#query').on('click', function () {
  	       		var pathName=$("#pathName").val();
  	       		console.info(pathName);
  	       		
  	       		var subStationName=$("#subStationName").val();
  	       		var subStationNameText = $("#regionName").val();
  	       		if(isNull(subStationNameText)){
  	       			console.info("xuanze");
  	       		}else{
  	       			console.info("tainxie");
  	       			subStationName = subStationNameText;
  	       		}
  	       		var mgrId=xiangmuId;
	            var startTime=$("#startTime").val();
                var endTime=$("#endTime").val();
                var countyId = $("#countyId").val();
                var regionName = $("#countyId option:selected").text();
	            var isChooseMap = $("input[name='isChooseMap']:checked").val();
	            var cityId=$("#cityId").val();
	            if(isNull(cityId)){
                    cityId="25";
                }
                if(isNull(mgrId)){
                    layer.msg("未获取到项目编号，请重新加载");
                    return false;
                }
                
                if (subStationName == null || subStationName== "") {
                            layer.msg("点位信息不能为空");
                            return false;
                        }
  	       		//console.info("----"+pathName+"---"+subStationName);
  	       		var url="../areaManage"+mapManage+"?cityId="+cityId+"&mgrId="+mgrId+"&sendStart="+startTime+"&sendEnd="+endTime
		            		+"&scopeType="+isChooseMap+"&mgrState="+"2"+"&checkState="+"2"+"&flag="+"insert";
		          	
	                
	                if(null!=countyId&&''!=countyId){
		          		url = url +"&countyId="+countyId;
	                }
	                if(null!=regionName&&''!=regionName){
	                	url = url +"&regionName="+encodeURI(regionName);
	                }
	                
	                if(null!=subStationName&&''!=subStationName){
		          		url = url +"&subStationName="+encodeURI(subStationName);
	                }
	                if(null!=pathName&&''!=pathName){
	                	url = url +"&pathName="+encodeURIComponent(pathName);
	                }
	                url=encodeURI(url); 
		            $('iframe').attr('src', url);
            });
  
            //获取mgr序列
            var xiangmuId;
			function getMgrId() {
			    var url = '/util/getMgrId';
			    $.ajax({
			            url: url,
			            type: 'post',
			            success: function (json) {
			                xiangmuId = json.data;
			            }, error: function () {
			                layer.msg('系统异常，请联系管理员处理!');
			            }
			        }
			    );
			}
			
			//判断基站是否重复
			function judgeStationRepeat(stationId,stationName){
				var url = '/xuYiStation/findStationById';
			    $.ajax({
			            url: url,
			            type: 'post',
			            async: false,
			            data: {
			              stationId: $("#stationId").val()
                        },
			            success: function (json) {
			                var stationInfo = json.data;
			                if(stationInfo==null){
			                    stationIsRepeat = false;
                            }else{
			                    stationIsRepeat = true;
                            }
			            }, error: function () {
			                layer.msg('系统异常，请联系管理员处理!');
			            }
			        }
			    );
			}
            //插入提交
            $('#submit').on('click', function () {
                var stationName = $("#stationName").val();
                var stationId = $("#stationId").val();
                var lng = $("#lng").val();
                var lat = $("#lat").val();
                var remark = $("#remark").val();
                
                console.info(stationName+stationId+remark+lng+lat+remark);
                var param = {
                    stationName : stationName ,
                    stationId : stationId,
                    lng : lng ,
                    lat : lat ,
                    remark : remark 
                };
                //var index = layer.load(1, {shade: [0.1,'#fff'] });//0.1透明度的白色背景
                var url='/xuYiStation/stationAdd';
                var index ;
                $.ajax({
                    type: 'post',
                    data: param,
                    url: url,
                    async: false,
                    beforeSend: function () {
                        if (isNull(stationName)) {
                            layer.msg("基站名称不能为空");
                            return false;
                        }
                        
                        if (isNull(stationId)) {
                            layer.msg("基站编码不能为空");
                            return false;
                        }else{
                            if(stationId.indexOf(":")==-1){
                                layer.msg("请输入正确的基站编码！");
                                return false;
                            }else{
                                var staionSpiltArr = stationId.split(":");
                                if(isNull(staionSpiltArr[0])||isNull(staionSpiltArr[1])){
                                    layer.msg("请输入正确的基站编码！");
                                    return false;
                                }
                            }
                        }
                        
                        if (isNull(lng)) {
                            layer.msg("基站经度不能为空");
                            return false;
                        }else{
                            if(lng.indexOf(".")==-1){
                                layer.msg("请输入正确的基站经度！");
                                return false;
                            }else{
                                var lngSpiltArr = lng.split(".");
                                if(isNull(lngSpiltArr[0])||isNull(lngSpiltArr[1])){
                                    layer.msg("请输入正确的基站经度！");
                                    return false;
                                }
                            }
                        }

                        if (isNull(lat)) {
                            layer.msg("基站纬度不能为空");
                            return false;
                        }else{
                            if(lat.indexOf(".")==-1){
                                layer.msg("请输入正确的基站纬度！");
                                return false;
                            }else{
                                var latSpiltArr = lat.split(".");
                                if(isNull(latSpiltArr[0])||isNull(latSpiltArr[1])){
                                    layer.msg("请输入正确的基站纬度！");
                                    return false;
                                }
                            }
                        }
                        if (!checkLng(lng)) {
                            layer.msg("基站经度必须为正确的数字");
                            return false;
                        }
                        
                        if (!checkLat(lat)) {
                            layer.msg("基站纬度必须为正确的数字");
                            return false;
                        }
                        if(stationName.length>20){
			              layer.msg("基站名过长，请修改");
			              return false;
			            }
                        judgeStationRepeat(stationId);
                        if(stationIsRepeat){
                            layer.msg("基站编码重复，请修改");
                            return false;
                        }

			            index = layer.load(1, {shade: [0.1,'#fff'] });//0.1透明度的白色背景
                    },
                    success: function (json) {
                        if (json.code == 0) {
                            layui.use('layer', function () {
                                layer.msg('添加成功,点击确认返回点位管理界面', {
                                    time: 0 //不自动关闭
                                    , btn: ['确定']
                                    , yes: function (index2) {
                                    	layer.close(index);
                                        layer.close(index2);
                                        location.href = "xuYiStationManage.html";
                                    }
                                });
                            }); 
                        }else {
                        	layer.close(index);
                            layer.msg(json.msg);
                        }
                    },
                    complete: function () {

                    },
                    error: function (data) {
                    	layer.close(index);
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });
            });
           
            
            //身份识别
            $(document).ready(function () {
            
            });
            //校验字符串
			function checkStr(str,defaultVal){
				if (typeof(str) == "undefined" || str == null || str == "null") {
					if (defaultVal == undefined) {
						str = "";
					} else {
						str = defaultVal;
					}
				}
				return str;
		    }
		    
		    //上传变电站文件
		    upload.render({
					elem : '#import2',
					url : '/xuYiStation/importStation',
					auto : false, //关闭自动上传
					accept : 'file', //普通文件
					exts : 'csv', //只允许上传csv文件 //,multiple: true
					bindAction : '#uploadFile2',
					before : function() {
						layer.load(); //上传loading
					},done : function(obj) {
						console.info(obj);
						layer.closeAll('loading'); //关闭loading
						if (obj.code == 0 && obj.data!=null) {
							var data = obj.data;
							layer.confirm('成功导入' +data + '个基站', {
								btn: ['确定'] //按钮
							}, function () {
								location.href="xuYiStationManage.html";
							});
    					}
						else{
			               l
			            }
						/* if (obj.code > 0) {
							layer.msg(obj.msg);
						} */
		
					},
					error : function(index, upload) {
						layer.closeAll('loading'); //关闭loading
						layer.msg("上传过程中出现异常，请重试");
					}
				});
		    
		    
		    form.on("radio(insertType)", function (data) {
		    	var insertType = $("input[name='insertType']:checked").val();
		    		//选择单个点位录入
				if(insertType==="single"){
		        	$("#singleInsert").show();
		        	$("#totalInsert").hide();
		        }else if(insertType==="total"){
		        	//选择批量点位录入
		        	$("#singleInsert").hide();
		        	$("#totalInsert").show();
		        }
		    });

            $('#back').on('click', function (){
                window.location.href="xuYiStationManage.html";
            });

            $('#back2').on('click', function (){
                window.location.href="xuYiStationManage.html";
            });
        });
    </script>
</head>
<body>
<div class="layui-html-limit">
    <!--container-->
    <div class="layui-content-body">
        <div class="scroll-box">
            <div class="breadcrumb-box">
              <span class="layui-breadcrumb" lay-separator="|">
                  <a href="javascript:returnIndex2()">首页&nbsp;</a>
                  <a href="#"> &nbsp;基站信息添加&nbsp; </a>
              </span>
            </div>
            <div class="content-box" style="height:100%;width:100%;">
                <div class="layui-form layui-form-pane" style="height:100%;width:110%;" >
                  <div class="layui-form-item" id="sendLimit" >
                    <label class="layui-form-label" style="width: auto;">基站录入方式&nbsp;</label>
                        <div class="layui-input-block">
                        	<input class="radioItem" lay-filter="insertType" type="radio" name="insertType" title="单个基站录入" onclick="ChooseRadio(this)" value="single"  checked="checked">
                            <input class="radioItem" lay-filter="insertType" type="radio" name="insertType" title="批量基站录入" onclick="ChooseRadio(this)" value="total">
                        </div>
                    </div>
                    <div style="width:91%;height:2px;background:#b2b2b2;margin-bottom:16px;" class="layui-block"></div>
                	<div id = "singleInsert">
                    <div class="layui-form-item" >
                        <div class="layui-block" pane="">
                            <label class="layui-form-label" style="width:120px">基站名称</label>
                            <div class="layui-input-block" style="width:180px;margin-left:120px;">
			                    <input type="text" name="stationName" id="stationName" required lay-verify="required"
			                           placeholder="请输入基站名称"
			                           autocomplete="off" class="layui-input">
			                </div>
                        </div>
                    </div>
                    <div class="layui-form-item" >
                        <div class="layui-block" pane="">
                            <label class="layui-form-label" style="width:120px">基站编码</label>
                            <div class="layui-input-block" style="width:180px;margin-left:120px;">
			                    <input type="text" name="stationId" id="stationId" required lay-verify="required"
			                           placeholder="请输入基站编码"
			                           autocomplete="off" class="layui-input">
			                </div>
                        </div>
                    </div>
                    
                     <div class="layui-form-item" >
                        <div class="layui-block" pane="">
                            <label class="layui-form-label" style="width:120px">基站经度</label>
                            <div class="layui-input-block" style="width:180px;margin-left:120px;">
			                    <input type="text" name="lng" id="lng" required lay-verify="required"
			                           placeholder="如：118.xxxxx"
			                           autocomplete="off" class="layui-input">
			                </div>
                        </div>
                    </div>
                    
                     <div class="layui-form-item" >
                        <div class="layui-block" pane="">
                            <label class="layui-form-label" style="width:120px">基站纬度</label>
                            <div class="layui-input-block" style="width:180px;margin-left:120px;">
			                    <input type="text" name="lat" id="lat" required lay-verify="required"
			                           placeholder="如：31.xxxxx"
			                           autocomplete="off" class="layui-input">
			                </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-block">
                            <label class="layui-form-label" style="width: auto">&nbsp;基站信息备注</label>
                            <div class="layui-input-block">
                            	<textarea placeholder="请输入内容" class="layui-textarea" style="width: 50%"
                                      id="remark"></textarea>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <!-- <button class="layui-btn" id="submit">立即提交</button> -->
                        <button class="layui-btn" id="submit">提交</button>
                        <button class="layui-btn" id="back">返回</button>
                    </div>
                    </div>
                    <div id="totalInsert" style="display:none;">
                    	<div class="layui-form-item import2">
								<br>
								<div class="layui-inline" >
									<button type="button" class="layui-btn" id="import2">
										<i class="layui-icon"></i>基站导入
									</button>
								</div>
								
								<div class="layui-form-item tips" style = "margin-bottom : 5px">
										<h4 class="tips-h4">
											<i class="iconfont icon-xinxi"></i>
											请先选择导入的文件再点击提交
										</h4>
								</div>
									<span><a href="/xuYiStation/apidownload2"><u>基站导入帮助手册</u></a></span><br>
									 <span><a href="/xuYiStation/modeldownload2"><u>基站导入模板下载</u></a></span>
									<br>
							<div class="layui-form-item upload2">
								<button class="layui-btn" id="uploadFile2">提交</button>
                                <button class="layui-btn" id="back2">返回</button>
							</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>